$(function () {

    $.post($.webPath() + '/base/sjzdsy/get_sjzdz', {'bm':'cd'}, function (cddata) {
        $.post($.webPath() + '/base/sjzdsy/get_sjzdz', {'bm':'jzfs'}, function (data) {
            kcgdGdGrid($.webPath() + '/kcgl/kcgd/kcgdGd_wzmx?khIds=' + $("#kcgdGd_khIds").val() || '', cddata, data);
        })
    });

})
function kcgdGdGrid(href, cdOptions, fsOptions) {
    $("#kcgdGd_mx").edatagrid({
        url:href,
        fitColumns:true,
        singleSelect:true,
        fit:false,
        width:1000,
        height:390,
        checkOnSelect:false,
        selectOnCheck:false,
        showFooter:false,
        rownumbers:true,
        columns:[
            [
                {field:'wzgg.code', title:'物资编码', width:100},
                {field:'wzgg.name', title:'物资名称', width:100},
                {field:'wzgg.ggxh', title:'规格型号', width:100},
                {field:'wzgg.dw', title:'单位', width:60},
                {field:'wsdj', title:'成本价(不含税)', formatter:function (value) {
                    var sl = formatJe(value, ",");
                    return sl;
                }, width:100},
                {field:'cd.z', title:'产地', width:50},
//                {field:'chd', title:'长度', width:50},
//                {field:'jz', title:'件重', width:50},
                {field:'kkdsl', title:'可供重量', formatter:function (value) {
                    var sl = formatSlForHtml(value, ",");
                    return sl;
                }, width:100},
                {field:'gdjs', editor:{type:'numberbox',
                    formatter:function (value) {
                        var sl = formatSlForHtml(value);
                        return sl;
                    }}, title:'挂单件数', width:50},
                {field:'gdsl', editor:{type:'numberbox', options:{min:0, precision:4}}, formatter:function (value) {
                    var sl = formatSlForHtml(value, ",");
                    return sl;
                }, title:'挂单重量', width:100},
                {field:'shj', editor:{type:'numberbox', options:{min:0, precision:4, required:true}},
                    formatter:function (value, row, index) {
                        var sl = formatSlForHtml(value);
                        return '<span class="shj_jsfs_tip" jsfs="' + row.jsfs + '">' + sl + '</span>';
                    }, title:'赎货挂牌价', width:50},
                {field:'lxjt', title:'优惠价格', width:50}
            ]
        ],
        loadFilter:function (data) {
            $(data.rows).each(function () {
                this.gdjs = (this.js == null) ? 0 : this.js;
                this.gdsl = this.kkdsl;
            })
            return data;
        },
        onSelect:function (rowIndex) {
            //获取每个单元格
            $("#kcgdGd_mx").edatagrid('editRow', rowIndex);
            var editors = $("#kcgdGd_mx").datagrid('getEditors', rowIndex);
            if (editors.length == 0)
                return;
            var gdjsEditor = editors[0];
            var gdslEditor = editors[1];
            var khdjEditor = editors[2];
            gdjsEditor.target.unbind('blur').bind('blur', function () {
                $(gdjsEditor.target).numberbox('setValue', $(gdjsEditor.target).val());
                var selected = $('#kcgdGd_mx').datagrid('getSelected');
                if (selected && selected['cd.id']) {
                    $.post($.webPath() + '/cggl/cgdgl/get_jz', {'ggId':selected['wzgg.id'], cdId:selected['cd.id']}, function (data) {
                        var obj = eval('(' + data + ')');
                        if (obj.jz) {
                            $(gdslEditor.target).numberbox('setValue', parseFloat(obj.jz) * $(gdjsEditor.target).numberbox('getValue'));
                        }
                    })
                } else {
                    $.messager.alert('提示', '产地不能为空', 'info');
                }
            });
            gdslEditor.target.unbind('blur').bind('blur', function () {
                $(gdslEditor.target).numberbox('setValue', $(gdslEditor.target).val());
                $(gdjsEditor.target).numberbox('setValue', $(gdjsEditor.target).val());
                $(khdjEditor.target).numberbox('setValue', $(khdjEditor.target).val());
            });
            khdjEditor.target.unbind('blur').bind('blur', function () {
                $(gdslEditor.target).numberbox('setValue', $(gdslEditor.target).val());
                $(gdjsEditor.target).numberbox('setValue', $(gdjsEditor.target).val());
                $(khdjEditor.target).numberbox('setValue', $(khdjEditor.target).val());
            });
        },
        onLoadSuccess:function () {
            $(".shj_jsfs_tip").each(function () {
                var jsfs = $(this).attr("jsfs");
                $(this).closest("td").tooltip({
                    position:'bottom',
                    content:'<span style="color:#ff0000">' + jsfs + '</span>'
                });
            });
        }
    })
}

//计算开单计量和开单金额
function countlldSum(index, count, je) {
    var sumkdsl = 0;
    var sumkddj = 0;
    var data = $("#lld_mx").edatagrid("getData");
    for (var i = 0; i < data.rows.length; i++) {
        var kdsl = parseFloat(data.rows[i]['kdsl']);
        var kddj = parseFloat(data.rows[i]['kddj']);
        if (i == index) {
            kdsl = parseFloat(count);
            kddj = parseFloat(je);
        } else {
            kdsl = isNaN(kdsl) ? 0 : kdsl;
            kddj = isNaN(kddj) ? 0 : kddj;
        }
        sumkdsl += kdsl;
        sumkddj += kddj;
    }
    addFooter(sumkdsl, sumkddj);
}

//在表底加载开单计量和开单金额的合计
function addFooter(sumkdsl, sumkddj) {
    var footer = [
        {"wzgg.code":"合计", "kdsl":sumkdsl, "kddj":sumkddj}
    ];
    if (footer != null)
        $("#lld_mx").edatagrid('reloadFooter', footer);
}

